<!-- 原辅包检验台账 -->
<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item>
        <el-select placeholder="请选择分类" size="mini" v-model="queryParam.category">
          <el-option v-for="item in categorys" :key="item.value" :label="item.text" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="timeRange" type="daterange" align="right" unlink-panels value-format="yyyy-MM-dd"
          range-separator="-" start-placeholder="请验时间段" end-placeholder="不包含结束时间" :picker-options="pickerOptions" :clearable="true" size="mini">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-select v-model="queryParam.status" clearable placeholder="全部状态" size="mini">
          <el-option label="待处理" value="0" />
          <el-option label="待取样" value="1" />
          <el-option label="待收样" value="2" />
          <el-option label="待分发" value="3" />
          <el-option label="检验中" value="4,5" />
          <el-option label="待结论" value="6" />
          <el-option label="待放行" value="7,10" />
          <el-option label="已完成" value="8,11" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-select placeholder="请选择供应商" v-model="queryParam.supplierId" :clearable="true" size="mini" filterable remote :remote-method="querySupplier">
          <el-option v-for="supplier in suppliers" :key="supplier.supplierId" :label="supplier.supplierName" :value="supplier.supplierId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select placeholder="请选择品名" v-model="queryParam.productId" :clearable="true" size="mini" filterable remote :remote-method="queryProduct">
          <el-option v-for="product in products" :key="product.productId" :label="product.productName+'/'+product.productGmp" :value="product.productId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input placeholder="批号" v-model="queryParam.batchNum" :clearable="true" size="mini"/>
      </el-form-item>
	  <el-form-item>
	     <el-select placeholder="请选择检验项" clearable collapse-tags multiple v-model="items" size="mini">
	       <el-option v-for="item in choiceHead" :key="item" :label="item" :value="item">
	       </el-option>
	     </el-select>
	   </el-form-item>
      <el-form-item>
        <el-button type="primary" class="pan-btn primary-btn" icon="el-icon-search" size="mini" @click="queryData">搜索</el-button>
      </el-form-item>
	  <el-form-item>
		  <el-button type="primary" class="pan-btn success-btn" icon="el-icon-download" size="mini" @click="exportData">导出</el-button>
	  </el-form-item>
    </el-form>
    <el-table :row-style="{height: '10px'}" :cell-style="{padding:'0px 0'}" :data="tableData" border style="font-size: 1rem;" v-loading="loading">
      <template v-for="(title,index) in tableHead">
        <el-table-column show-overflow-tooltip :prop="title" :label="title" :key="index" width="150px"/>
      </template>
    </el-table>

    <el-pagination
      background
	  :page-size="20"
      layout="total,prev, pager, next"
      :total="totalPage"
      @current-change="handleCurrentChange">
    </el-pagination>

  </div>
</template>

<script>
  import {
    yfbCheckData,
		visit,
		yfbExport
  } from '@/api/tables'
  import {
    getProductList,
    getProductCategorys
  } from '@/api/product'
  import {
    getSupplierList
  } from '@/api/supplier'

  export default {
    data() {
      return {
        loading:false,
        totalPage:0,
        timeRange: "",
        items: "",
        queryParam: {
          pageIndex:1,
          pageSize:20,
          category: 1,
          startTime: "",
          endTime: "",
          supplierId: "",
          productId: "",
          productSpec: "",
          batchNum: "",
          gmpNum: "",
		  status:""
        },
        suppliers:[],
        products:[],
        tableHead: [],
        choiceHead:['品名','规格','供应商','批号','原厂批号'],
        tableData: [],
        categorys: [{
          text: "原料",
          value: 1
        }, {
          text: "辅料",
          value: 2
        }, {
          text: "包材",
          value: 3
        }],
        pickerOptions: {
          shortcuts: [{
            text: '本月',
            onClick(picker) {
              picker.$emit('pick', [new Date(), new Date()]);
            }
          }, {
            text: '今年至今',
            onClick(picker) {
              const end = new Date();
              const start = new Date(new Date().getFullYear(), 0);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近六个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setMonth(start.getMonth() - 6);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
      }
    },
    watch:{
      items: function(newData, oldData){
		this.tableHead = newData;
      }
    },
    mounted() {
			visit("原辅包检验台账查询").then().catch()
      this.searchData()
    },
    methods: {
      queryData(){
        this.queryParam.pageIndex = 1;
        this.searchData()
      },
			exportData(){
				if(this.queryParam.productId == ''){
					this.$message.error("请先选择品名信息")
					return
				}
				window.open('/api/yfbCheckTable/export/'+ this.queryParam.category +'/' + this.queryParam.productId, '_blank')
			},
      handleCurrentChange(val){
        this.queryParam.pageIndex = val;
        this.searchData()
      },
      searchData() {
        this.loading = true;
        if (this.timeRange != '') {
          this.queryParam.startTime = this.timeRange[0]
          this.queryParam.endTime = this.timeRange[1]
        }
        yfbCheckData(this.queryParam).then(res => {
          this.tableHead = res.tableHead
          this.tableData = res.tableData
          this.choiceHead = res.tableHead
          this.totalPage = res.total
          this.loading = false;
        })
      },
      querySupplier(query){
        let param = {
          page:1,
          limit:10,
          supplierName:query
        }
        getSupplierList(param).then(res => {
          this.suppliers = res.page.list
        })
      },
      queryProduct(query){
        let param = {
          page:1,
          limit:10,
          productName:query,
          productCategoryId:this.queryParam.category
        }
        getProductList(param).then(res => {
          this.products = res.page.list
        })
      }
    }
  }
</script>

<style>

</style>
